package github.ziwukun.jsonql.orm;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import github.ziwukun.jsonql.utils.CollectionUtils;

/**
 * 模型实现类
 * @author Hugo.Yu
 */
class ModelImpl implements Model{
	private static final long serialVersionUID = 1L;
	String modelName;
	CtnProp id;
	List<Prop> props = new LinkedList<>();
	List<CtnProp> ctnProps = new LinkedList<>();
	List<JoinProp> joinProps = new LinkedList<>();
	List<FmProp> fmProps = new LinkedList<>();
	List<RltProp> rltProps = new LinkedList<>();
	ContainerImpl ctn;
	
	ModelImpl(ModelBuilder builder){
		ModelBuilder.instanceModel(builder,this);
		for (Prop prop : props) {
			if(prop instanceof CtnProp) {
				ctnProps.add((CtnProp)prop);
			}
			else if(prop instanceof JoinProp) {
				joinProps.add((JoinProp)prop);
			}
			else if(prop instanceof FmProp) {
				fmProps.add((FmProp)prop);
			}
			else if(prop instanceof RltProp) {
				rltProps.add((RltProp)prop);
			}
		}
	}
	
	@Override
	public String modelName() {
		return modelName;
	}
	
	@Override
	public CtnProp id() {
		return id;
	}

	@Override
	public List<Prop> listProps() {
		return new ArrayList<>(props);
	}
	
	@Override
	public boolean isPropEmpty() {
		return props.isEmpty();
	}

	@Override
	public Prop getPropByName(String propName) {
		if(StrUtil.isBlank(propName)) {
			return null;
		}
		return CollectionUtils.findOne(props, prop -> {
			return prop.propName().equals(propName);
		});
	}
	@Override
	public Container ctn() {
		return ctn;
	}

	@Override
	public List<CtnProp> listCtnProps() {
		return new ArrayList<>(ctnProps);
	}

	@Override
	public boolean isCtnPropEmpty() {
		return ctnProps.isEmpty();
	}

	@Override
	public List<JoinProp> listJoinProps() {
		return new ArrayList<>(joinProps);
	}

	@Override
	public boolean isJoinPropEmpty() {
		return joinProps.isEmpty();
	}

	@Override
	public List<FmProp> listFmProps() {
		return new ArrayList<>(fmProps);
	}

	@Override
	public boolean isFmPropEmpty() {
		return fmProps.isEmpty();
	}

	@Override
	public List<RltProp> listRltProps() {
		return new ArrayList<>(rltProps);
	}

	@Override
	public boolean isRltPropEmpty() {
		return rltProps.isEmpty();
	}
}
